(function($){
	function getPluginName(target){
		if ($(target).data('treegrid')){
			return 'treegrid';
		} else {
			return 'datagrid';
		}
	}
	var extendedOptions = {
		defaultFilterOptions: {
			onInit: function(target){
				var name = getPluginName(target);
				var opts = $(target)[name]('options');
				var filterOpts = this.filterOptions;
				var field = $(this).attr('name');
				var input = $(this);
				input.focus(function(){
					nowFilterTable = target;
				});
				var control = $(this);
				var control_optsMode =  true;
				if(filterOpts.type == 'combo' || filterOpts.type == 'combobox' || filterOpts.type == 'combotree' || filterOpts.type == 'combogrid' || filterOpts.type == 'combotreegrid'){
					var control_opts = control[filterOpts.type]('options');
					var onClick = null;
					var onChange=null;
					var filter = null;
					if(control_opts.mode){
						control_optsMode = false;
					}
					if(typeof control_opts.onClick == 'function'){
						onClick = control_opts.onClick;
						
					}
					if(typeof control_opts.onChange == 'function'){
						onChange = control_opts.onChange;

					}
					if(typeof control_opts.filter == 'function'){
						filter = control_opts.filter;

					}
					control[filterOpts.type]({
						onClick: function(newValue, oldValue){
							nowFilterTable = target;
							if (this.timer){
								clearTimeout(this.timer);
							}
							if (opts.filterDelay){
								this.timer = setTimeout(function(){
									_doFilter();
								}, opts.filterDelay);
							} else {
								_doFilter();
							}
							if(typeof onClick == 'function') {
								onClick.call(control,newValue, oldValue);
							}
						},
						onChange: function(newValue, oldValue){
							nowFilterTable = target;
							var rule = $(target)[name]('getFilterRule', field);
							if(!newValue){
								var op = rule ? rule.op : (filterOpts ? filterOpts.defaultFilterOperator || opts.defaultFilterOperator : opts.defaultFilterOperator);
								$(target)[name]('addFilterRule', {
									field: field,
									// op: opts.defaultFilterOperator,
									op: op,
									value: ""
								});
								if(opts.automaticPolling !== false)
									$(target)[name]('doFilter');
							}
							if(typeof onChange == 'function') {
								onChange.call(control,newValue, oldValue);
							}
						},
						filter:function(q, row){
							var opts = control.combobox('options');
							return row[opts.textField].indexOf(q)!= -1 ;
						}
					});
					control.parent().find("input[type='text']").bind('keydown.filter',function (e) {
						$(target)[name]('doFilter');
					})
				}
				if (input.data('textbox')){
					input = input.textbox('textbox');
				}
				input.unbind('.filter').bind('keydown.filter ', function(e){
					nowFilterTable = target;
					if(control_optsMode) {
						var t = $(this);
						if (this.timer) {
							clearTimeout(this.timer);
						}
						if (e.keyCode == 13) {
							_doFilter();
						} else if (opts.filterDelay) {
							this.timer = setTimeout(function () {
								_doFilter();
							}, opts.filterDelay);
						}
					}
				});
				function _doFilter(){
					var rule = $(target)[name]('getFilterRule', field);
					var value;
					if(filterOpts.type == 'textbox'){
						value = input.val();
					} else {
						value = control[filterOpts.type]("getValue");
					}
						if (value != '') {
							if ((rule && rule.value != value) || !rule) {
								var op = rule ? rule.op : (filterOpts ? filterOpts.defaultFilterOperator || opts.defaultFilterOperator : opts.defaultFilterOperator);
								$(target)[name]('addFilterRule', {
									field: field,
									// op: opts.defaultFilterOperator,
									op: op,
									value: value
								});
								if(opts.automaticPolling !== false)
									$(target)[name]('doFilter');
							}
						} else {
							if (rule) {
								$(target)[name]('removeFilterRule', field);
								if(opts.automaticPolling !== false)
									$(target)[name]('doFilter');
							}
						}
				}
			}
		}
	};
	$.extend($.fn.datagrid.defaults, extendedOptions);
	$.extend($.fn.treegrid.defaults, extendedOptions);
})(jQuery);